import tkinter as tk
from tkinter import ttk

class MyApplication:
    def __init__(self, root):
        self.root = root
        self.components = [
            {'type': 'label', 'text': 'Name:'},
            {'type': 'entry', 'name': 'name_entry'},
            {'type': 'label', 'text': 'Age:'},
            {'type': 'entry', 'name': 'age_entry'},
            {'type': 'label', 'text': 'Gender:'},
            {'type': 'combobox', 'name': 'gender_combobox', 'values': ['Male', 'Female', 'Other']}
        ]
        self.create_panel()

    def create_panel(self):
        panel_frame = ttk.Frame(self.root)
        panel_frame.pack(padx=10, pady=10)

        # 清空现有面板
        for widget in panel_frame.winfo_children():
            widget.destroy()

        label_idx = 0
        input_vars = {}

        # 创建面板组件
        for idx, component in enumerate(self.components):
            label_text = component.get('text', '')
            component_type = component.get('type', '')

            if component_type == 'label':
                label = ttk.Label(panel_frame, text=label_text)
                label.grid(row=idx, column=0, padx=5, pady=5, sticky="w")
                label_idx = idx

            elif component_type == 'entry':
                label_name = component.get('name', '')
                label = ttk.Label(panel_frame, text=label_text)
                label.grid(row=label_idx, column=0, padx=5, pady=5, sticky='e')
                var = tk.StringVar()
                entry = ttk.Entry(panel_frame, textvariable=var)
                entry.grid(row=label_idx, column=1, padx=5, pady=5, sticky='w')
                input_vars[label_name] = var

            elif component_type == 'combobox':
                label_name = component.get('name', '')
                label = ttk.Label(panel_frame, text=label_text)
                label.grid(row=label_idx, column=0, padx=5, pady=5, sticky='e')
                values = component.get('values', [])
                var = tk.StringVar()
                combobox = ttk.Combobox(panel_frame, values=values, textvariable=var)
                combobox.grid(row=label_idx, column=1, padx=5, pady=5, sticky='w')
                input_vars[label_name] = var

        # 创建按钮
        button_frame = ttk.Frame(panel_frame)
        button_frame.grid(row=len(self.components), columnspan=2, pady=10)

        default_buttons = [
            {'label': '确认', 'command': self.run_feature},
            {'label': '清空输入', 'command': self.clear_inputs}
        ]
        for idx, button_info in enumerate(default_buttons):
            button_label = button_info.get('label', '')
            button_command = button_info.get('command', None)
            if button_command:
                button = ttk.Button(button_frame, text=button_label, command=button_command)
                button.grid(row=0, column=idx, padx=10)

        # 设置日志显示框到 FeatureClass
        self.log_display = tk.Text(panel_frame, height=5, width=50)
        self.log_display.grid(row=len(self.components) + 1, columnspan=2, padx=10, pady=10)
        self.set_log_display(self.log_display)

        # 保存输入变量
        self.input_vars = input_vars

    def run_feature(self):
        # 示例功能：获取输入并打印日志
        log_text = ""
        for name, var in self.input_vars.items():
            log_text += f"{name}: {var.get()}\n"
        self.log_display.insert(tk.END, log_text)

    def clear_inputs(self):
        # 清空输入框内容
        for var in self.input_vars.values():
            var.set("")

    def set_log_display(self, log_display):
        # 设置日志显示框
        self.log_display = log_display


if __name__ == "__main__":
    root = tk.Tk()
    app = MyApplication(root)
    root.mainloop()
